package com.crm.finance.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizFinanceCostclass;
import com.crm.model.BizFinancePayee;
import com.crm.model.BizProject;
import com.crm.model.FinanceZpExpend;
import com.crm.model.SysUser;
import com.crm.service.BizFinanceCostclassService;
import com.crm.service.BizFinancePayeeService;
import com.crm.service.BizProjectService;
import com.crm.service.FinanceZpExpendService;
import com.crm.service.SysUserService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/financeZpExpend")
public class FinanceZpExpendController {

	@Autowired
	private  FinanceZpExpendService financeZpExpendService;
	
	@Autowired
	private BizProjectService projectService;
	
	@Autowired
	private BizFinanceCostclassService costclassService;
	
	@Autowired
	private BizFinancePayeeService payeeService;
	
	@Autowired
	private SysUserService userService;
	
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response){
		
		SysUser user = (SysUser) SessionUtils.get(SessionModel.SESSION_USER);
		
		request.setAttribute("currUser", user);
		
		return "financeCenter/zpExpend";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<FinanceZpExpend> page, FinanceZpExpend t){
		
		Page<FinanceZpExpend> list = (Page<FinanceZpExpend>) financeZpExpendService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(FinanceZpExpend record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpExpendService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(FinanceZpExpend record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpExpendService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}

	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(FinanceZpExpend record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpExpendService.deleteById(record.getZpExpendId());
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/showProject")
	@ResponseBody
	public void showProject(HttpServletRequest request,HttpServletResponse response){
		
		Page<BizProject> page = new Page<BizProject>();
		
		List<BizProject> list = projectService.selectAll(page);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/showPCostclass")
	@ResponseBody
	public void showPCostclass(HttpServletRequest request,HttpServletResponse response){
		
		Page<BizFinanceCostclass> page = new Page<BizFinanceCostclass>();
		
		BizFinanceCostclass costclass = new BizFinanceCostclass();
		
		costclass.setParentId("0");
		
		List<BizFinanceCostclass> list = costclassService.selectAll(page, costclass);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/showCostclass")
	@ResponseBody
	public void showCostclass(HttpServletRequest request,HttpServletResponse response,String financeCostclassId){
		
		Page<BizFinanceCostclass> page = new Page<BizFinanceCostclass>();
		
		BizFinanceCostclass costclass = new BizFinanceCostclass();
		
		costclass.setParentId(financeCostclassId);
		
		List<BizFinanceCostclass> list = costclassService.selectAll(page, costclass);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/getTotalIncome")
	@ResponseBody
	public void getTotalIncome(HttpServletResponse response,HttpServletRequest request,FinanceZpExpend t){
		
		FinanceZpExpend expend = financeZpExpendService.getTotalIncome(t);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(expend));
	}
	
	@RequestMapping("/showPayee")
	@ResponseBody
	public void showPayee(HttpServletRequest request,HttpServletResponse response){
		
		Page<BizFinancePayee> page = new Page<BizFinancePayee>();
		
		List<BizFinancePayee> list = payeeService.selectAll(page);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/showPayeeInfo")
	@ResponseBody
	public void showPayeeInfo(HttpServletRequest request,HttpServletResponse response,String financePayeeId){
		
		BizFinancePayee payee = payeeService.selectByPrimaryKey(financePayeeId);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(payee));
	}
	
	@RequestMapping("/showUser")
	@ResponseBody
	public void showUser(HttpServletRequest request,HttpServletResponse response,String userId){
		
		SysUser user = userService.selectByPrimaryKey(userId);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(user));
	}
}
